import {handlerGobackClick, hint} from "../../../utils/util";
import User from "../../../models/user";

const user = new User()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:"",
    password:""
  },
  onLoad(options){
    this.setData({
      phone:options.phone
    })
  },
  onReady: function () {
  },
  handlerGobackClick,
  inputPassword(e){
    this.setData({
      password:e.detail.value
    })
  },
  goLogin(){
    if(!this.data.phone){
      hint("手机号不能为空")
      return
    }
    if(!this.data.password){
      hint("密码不能为空")
      return
    }
    user.setUserPassword(this.data.phone,this.data.password)
        .then((res)=>{
          if(res.success){
            user.userLogin(this.data.phone,this.data.password)
                .then((res) => {
                  hint("请求成功")
                  wx.setStorageSync("token",res.messageBody.token)
                  wx.setStorageSync("user",res.messageBody.user)
                  wx.switchTab({
                    url:"/pages/home/index"
                  })
                })
                .catch((err) => {
                  console.log(err);
                })
          }
        })
        .catch((err)=>{
          console.log(err);})
  }
})
